package com.ruoyi.system.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.tools.BooleanMessage;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.UUID;

@Service
public class MessageCenterService {

    // 发送短信
    @Transactional
    public BooleanMessage send_piliang(String[] ppPhones , String ppMessageContent) {

        if(ppPhones.length<1) {
            return BooleanMessage.getErrorMessage("手机号不能为空");
        }
        if("".equals(ppMessageContent)) {
            return BooleanMessage.getErrorMessage("短信内容不能为空");
        }

        for(int i = 0; i < ppPhones.length; i++) {
            String messageid = UUID.randomUUID().toString();
//            if(!addSMS(messageid, ppPhones[i], "", ppMessageContent)) {
//                throw new RuntimeException("新增短信失败");
//            }
            BooleanMessage bm = send短信Post(ppPhones[i],ppMessageContent);
            if(!bm.isOk()) {
                throw new RuntimeException("发送短信失败");
            }
        }

        return BooleanMessage.getSuccessMessage("发送成功");
    }

    public BooleanMessage send短信Post(String pp号码, String pp内容) {
        HttpURLConnection conn = null;
        StringBuilder buf = new StringBuilder();
        OutputStream os = null;
        InputStream is = null;
        PrintWriter pw = null;
        BufferedReader br = null;
        OutputStreamWriter ow = null;
        try {
            String encode = URLEncoder.encode(pp内容,"utf-8");
            encode =URLEncoder.encode(encode,"utf-8");
            URL connectUrl;


            connectUrl = new URL("http://dx.12333.gov.cn/sms/send/smsSend.action");
            conn = (HttpURLConnection) connectUrl.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            int mm时间戳分 = (int)(System.currentTimeMillis() / 1000 / 60);
            conn.setConnectTimeout(mm时间戳分);
            conn.setReadTimeout(mm时间戳分);
            conn.connect();
            os = conn.getOutputStream();
            ow = new OutputStreamWriter(os, "utf-8");
            pw = new PrintWriter(ow, true);
            String mmUUID = UUID.randomUUID().toString();
            pw.println( "uuid="+mmUUID+"&timeOut="+mm时间戳分+"&phone="+pp号码+"&isLong=1&content="+encode+"&appName=btrrbsxt&smsKey=Btbs@530&businessName=m1");
            pw.flush();
            is = conn.getInputStream();
            br = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                buf.append(line);
            }


        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                }
            }
            if(ow!=null) {
                try {
                    ow.close();
                } catch (IOException e) {
                }
            }
            if (pw != null) {
                try {
                    pw.close();
                } catch (Exception e) {
                }
            }
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                }
            }
        }

        JSONObject mm回参 = JSONObject.parseObject(buf.toString()); // 转为json对象
        int mm编码 = mm回参.getIntValue("code");
        if(mm编码==1){
            return BooleanMessage.getSuccessMessage("发送成功！");
        }else{
            return BooleanMessage.getErrorMessage(mm回参.getString("message"));
        }
    }

}
